//
// Created by hrg on 2019/12/5.
//

#ifndef ECDRIVER_XMLPARSE_H
#define ECDRIVER_XMLPARSE_H

#include <mxml.h>
#include "mxml-private.h"
#include <ecrt.h>
#include <stdio.h>
#include <string>
#include <map>
#include "config.h"

class EcXml{
public:
    EcXml();
    ~EcXml();

    bool open(const char* filename);
    
    bool match(ec_slave_info_t slvinfo);

    bool IsModule(ec_slave_info_t slvinfo);

    bool GetModuleSyncInfo(ec_sync_info_t** syncInfo, const char* moduleId, int slot);

    bool GetServoSyncInfo(ec_sync_info_t** syncInfo);

    const char* ProfileNo();

    EcType GetType();

    void close();
protected:
    bool CreatePdo(mxml_node_t* node, const char* element, ec_sync_info_t* syncInfo);
    int  GetPdoEntry(mxml_node_t* node, uint16_t& pdoindex, std::map<std::pair<uint16_t, uint8_t>, uint8_t>& entrymap);
    bool CreatePdo(mxml_node_t* node, ec_pdo_info_t* pdo_info);
    bool CreatePdoEntry(ec_pdo_info_t* pdo_info, int pdoindex, std::map<std::pair<uint16_t, uint8_t>, uint8_t>& currmap);
private:
    FILE* m_File_;
    mxml_node_t* m_Tree_;
    mxml_node_t* m_EtherCATInfo_;

    int m_Slot_;
    int m_IndexIncrement_;
    int m_PdoIncrement_;

    std::map<std::pair<uint16_t, uint8_t>, uint8_t> m_PdoEntryMap_;
};
#endif //ECDRIVER_XMLPARSE_H
